﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMLib;

namespace BrainMatch
{
    public partial class LabelNamesEditor : Form
    {
        private WaveManager m_Manager = null;
        
        public LabelNamesEditor(WaveManager manager)
        {
            m_Manager = manager;
            InitializeComponent();
        }
        private void LoadShapes(int id)
        {
            listView1.Items.Clear();
            imageList1.Images.Clear();

            WaveItem wave = new WaveItem();
            DataRow[] rows = m_Manager.Dataset.labels.Select(string.Format("name_id={0}", id));
            Pen pen = new Pen(Color.FromArgb(100, 255, 0, 0), 1);

            foreach (var item in rows)
            {
                ListViewItem li = new ListViewItem();
                li.ImageIndex = imageList1.Images.Count;
                li.Tag = item;

                double[] data = BMUtility.StringToDoubles(item["graphics_data"].ToString());
                Bitmap bitmap = new Bitmap(imageList1.ImageSize.Width, imageList1.ImageSize.Height, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bitmap);
                g.Clear(Color.White);

                wave.SetData(data);
                wave.DrawToBitmap(bitmap, pen, true);
                imageList1.Images.Add(bitmap);

                listView1.Items.Add(li);
            }
        }
        //private void LabelNamesEditor_Load(object sender, EventArgs e)
        //{
        //    if (m_Manager == null) return;
        //    try
        //    {
        //        dataGridView1.DataSource = m_Manager.Dataset.label_names;
        //    }
        //    catch (Exception ex)
        //    {

        //        MessageBox.Show(ex.Message);
        //    }

        //    dataGridView2.DataSource = m_Manager.Dataset.label_quality;
        //}

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "fenlei_color" && e.RowIndex < dataGridView1.Rows.Count)
            //if (e.ColumnIndex == 2&&e.RowIndex<dataGridView1.Rows.Count)
            {
                if (e.RowIndex < 0) return;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                    if (row.DataBoundItem == null) return;
                    (row.DataBoundItem as DataRowView).Row["color"] = BMUtility.ColorToString(colorDialog1.Color);
                }
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.DataBoundItem == null) continue;
                int id = (int)(row.DataBoundItem as DataRowView).Row["id"];
                row.Cells["fenlei_color"].Style.BackColor = BMUtility.StringToColor((row.DataBoundItem as DataRowView).Row["color"].ToString());

                int count = m_Manager.Dataset.labels.Select(string.Format("name_id={0}", id)).Length;
                row.Cells["count"].Value = count.ToString();
                Debug.WriteLine(count);
            }
            
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            m_Manager.SaveData();
            base.OnClosing(e);
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0) return;
            if (dataGridView1.SelectedRows[0].DataBoundItem == null) return;
            LoadShapes((int)(dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row["id"]);
        }

        private void OnDeleteShapes(object sender, EventArgs e)
        {

            foreach (ListViewItem item in listView1.SelectedItems)
            {
                WaveDataSet.labelsRow row = item.Tag as WaveDataSet.labelsRow;
                if (row == null) continue;

                //m_Manager.Dataset.labels.Rows.Remove(row);
                //row.Delete();
                m_Manager.DeleteLabel(row.id);

                //m_Manager.DeleteLabel(row.id);
                listView1.Items.Remove(item);
            }
            m_Manager.Dataset.labels.AcceptChanges();
        }

        private void LabelNamesEditor_Load_1(object sender, EventArgs e)
        {
            if (m_Manager == null) return;
            try
            {
                dataGridView1.DataSource = m_Manager.Dataset.label_names;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

            dataGridView2.DataSource = m_Manager.Dataset.label_quality;
        }
    }
}
